package com;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteExcel {
    public static void writeExcel() throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("Name");
        headerRow.createCell(1).setCellValue("Age");

        Row dataRow = sheet.createRow(1);
        dataRow.createCell(0).setCellValue("John Doe");
        dataRow.createCell(1).setCellValue(30);

        FileOutputStream fileOut = new FileOutputStream(new File("e://zc.xlsx"));
        workbook.write(fileOut);
        fileOut.close();
        workbook.close();
    }
    public static void readExcel() throws IOException, InvalidFormatException {
        XSSFWorkbook sheets = new XSSFWorkbook(new FileInputStream("e://zc.xlsx"));
        XSSFSheet sheet = sheets.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <  lastRowNum+1; i++) {
            XSSFRow row = sheet.getRow(i);
            XSSFCell cell1 = row.getCell(0);
            XSSFCell cell2 = row.getCell(1);
            System.out.println(cell1 + " " + cell2);
        }
    }
    public static void main(String[] args) throws IOException {
//        writeExcel();
        try {
            readExcel();
        } catch (InvalidFormatException e) {
            throw new RuntimeException(e);
        }
    }
}
